﻿using FSM.IBll;
using FSM.Model;
using FSM.Web.Models;
using FSM.Web.Models.Fileter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FSM.Web.Controllers.BaseInfo
{
    /// <summary>
    /// 创建：宙冰
    /// 时间：2020/12/04
    /// 功能：系统菜单管理
    /// </summary>
    [Authorize]
    public class SysMenuController : BaseController
    {
        #region 模块
        private readonly DataResultModel drm;

        private readonly ISysMenuBll sysMenuBll;

        private readonly IRelMenuActionBll _relMenuActionBll;

        #endregion

        #region 构造函数

        public SysMenuController(ISysMenuBll sysMenuBll, IRelMenuActionBll relMenuActionBll)
        {
            this.sysMenuBll = sysMenuBll;

            this._relMenuActionBll = relMenuActionBll;

            ModuleNo = "0102";

            drm = new DataResultModel();
        }

        #endregion

        #region 菜单

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0102","Query")]
        public JsonResult Get(string id)
        {
            sys_menu model = sysMenuBll.Get(id);
            drm.success = true;
            drm.data = model;
            return drm.ToJson();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="menu_name">菜单名称</param>
        /// <param name="menu_pno">上级菜单编号</param>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0102", "Query")]
        public JsonResult List(string menu_name, string menu_pno, int? page = 1, int? page_size = 30)
        {
            Tuple<List<sys_menu>, int> result = sysMenuBll.List(menu_name, menu_pno, page.Value, page_size.Value);
            drm.success = true;
            drm.data = result.Item1;
            drm.total = result.Item2;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 获得菜单树信息
        /// </summary>
        /// <param name="p_no">上级编号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0102", "Query")]
        public JsonResult Tree(string p_no)
        {
            List<sys_menu> treeList = sysMenuBll.Tree(p_no);
            drm.success = true;
            drm.data = treeList;
            return drm.ToJson();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0102", "Add")]
        public JsonResult Insert(sys_menu model)
        {
            model.creater = AuthencationHelper.GetLoginUserNo(User);
            bool success = sysMenuBll.Insert(model);
            drm.success = success;
            drm.data = model;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0102", "Edit")]
        public JsonResult Update(sys_menu model)
        {
            model.creater = AuthencationHelper.GetLoginUserNo(User);
            bool success = sysMenuBll.Update(model);
            drm.success = success;
            drm.data = model;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="user_no">用户账号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0102", "Delete")]
        public JsonResult Delete(string user_nos)
        {
            bool success = sysMenuBll.Delete(user_nos);
            drm.success = success;
            return new JsonResult(drm);
        }

        #endregion

        #region 动作

        /// <summary>
        /// 获得指定菜单的动作信息
        /// </summary>
        /// <param name="id">菜单编号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0102", "Power")]
        public JsonResult GetActions(string id)
        {
            Tuple<List<rel_menu_action>, int> result = _relMenuActionBll.List(id, "", 1, 100);
            drm.success = true;
            drm.data = result.Item1;
            return drm.ToJson();
        }

        /// <summary>
        /// 赋于指定菜单动作权限
        /// </summary>
        /// <param name="id">菜单编号</param>
        /// <param name="act_nos">动作编号字串，以","分割</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0102", "Power")]
        public JsonResult GrantActions(string id, string act_nos)
        {
            string userNo = AuthencationHelper.GetLoginUserNo(User);
            drm.success = _relMenuActionBll.BatchInsert(userNo, id, (act_nos ?? "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList());
            if (!drm.success)
            {
                drm.message = "动作权限赋于失败";
            }
            return drm.ToJson();
        }

        #endregion
    }
}
